package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.base.response.JsonResult;
import cn.tedu._05mvcboot02.base.response.StatusCode;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.UserDto;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserVo;
import cn.tedu._05mvcboot02.pojo.vo.UserVo1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
@RestController
@RequestMapping("users/")
@Api(tags = "用户")
public class UserController {
    @Autowired(required = false)
    private UserMapper userMapper;


    @ApiOperation(value = "登录")
    @GetMapping("login")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userName",value = "用户名",required = true),
            @ApiImplicitParam(name = "passWord",value = "密码",required = true)
    })
    public JsonResult Login(String userName, String passWord){
        User user=userMapper.selectUser(userName);
        if (user!=null){
            String dbpassWord = user.getPassWord();
            if (dbpassWord.equals(passWord)){
                return JsonResult.ok();
            }
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        }
        return new JsonResult(StatusCode.USERNAME_ERROR);
    }

    @ApiOperation(value = "注册")
    @PostMapping("register")
    public JsonResult insertUser(UserDto userDto){
        User user = userMapper.selectUser(userDto.getUserName());
        if (user!=null)
            return new JsonResult(StatusCode.USERNAME_EXITS);
        user=new User();
        BeanUtils.copyProperties(userDto,user);
        int i = userMapper.insertUser(user);
        if (i==1)
        return JsonResult.ok();
        return new JsonResult(StatusCode.POERATION_FAILED);
    }


    @ApiOperation(value = "注销")
    @PostMapping("destory")
    @ApiImplicitParam(name = "userName",value = "用户名",required = true)
    public JsonResult deleteUser(String userName){
        User user=userMapper.selectUser(userName);
        if (user!=null) {
            userMapper.deleteUser(userName);
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.USERNAME_ERROR);
    }


    @ApiOperation(value = "更新")
    @PostMapping("update")
    public JsonResult updateUser(UserDto userDto){
        User user=userMapper.selectUser(userDto.getUserName());
        if (user!=null){
            int i = userMapper.updateUser(userDto);
            if (i==1)
            return JsonResult.ok();
            return new JsonResult(StatusCode.POERATION_FAILED);
        }
        return new JsonResult(StatusCode.USERNAME_ERROR);
    }


    @ApiOperation(value = "获取用户列表")
    @GetMapping("userlist")
    public JsonResult userList(){
        return JsonResult.ok(userMapper.selectUsers());
    }



    @ApiOperation(value = "ID查询用户")
    @GetMapping("selectById/{uid}")
    @ApiImplicitParam(name = "uid",value = "用户ID",required = true)
    public JsonResult selectById(@PathVariable Integer uid){
        return JsonResult.ok(userMapper.selectById(uid));
    }

}
